package com.atlin.project.fileService;

import cn.hutool.core.io.FileUtil;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.config.AtLinConfig;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.enums.FileUploadBizEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author atlin
 * @description 文件存储服务
 * @createDate 2024/12/30 21:03
 */
@Slf4j
public abstract class BaseFileService {

    private static final List<String> IMAGE_TYPE = Arrays.asList("jpeg", "jpg", "svg", "png", "webp");

    private AtLinConfig atLinConfig;

    public abstract AtLinConfig getConfig();

    public abstract String doSave(MultipartFile multipartFile, SysUser loginUser, FileUploadBizEnum fileUploadBizEnum);

    public abstract void doRead(String path, HttpServletRequest request, HttpServletResponse response);

    protected void configCheck() {
        if (atLinConfig == null) {
            this.atLinConfig = getConfig();
        }

        if (atLinConfig == null) {
            String errorMsg = "[文件服务配置错误]";
            log.error(errorMsg);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, errorMsg);
        } else {
            String bucket = atLinConfig.getBucket();
            String fileService = atLinConfig.getFileService();
            if (StringUtils.isAnyBlank(bucket, fileService)) {
                String errorMsg = "[文件服务参数配置缺失]";
                log.error(errorMsg);
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, errorMsg);
            }
        }
    }

    protected void fileCheck(FileUploadBizEnum fileUploadBizEnum, MultipartFile multipartFile) {
        if (fileUploadBizEnum == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持此业务类型");
        }
        switch (fileUploadBizEnum) {
            case USER_AVATAR:
                userAvatarCheck(multipartFile);
                break;
            case USER_IMPORT:
                userImportCheck(multipartFile);
                break;
            default:
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持此业务类型");
        }
    }


    protected String buildPath(long userId, String bizType) {
        return String.format("%s/%s/%s/%s", atLinConfig.getBucket(), bizType, userId, System.currentTimeMillis());
    }


    private void userAvatarCheck(MultipartFile multipartFile) {
        // 文件大小
        long fileSize = multipartFile.getSize();
        // 文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        final long ONE_MB = 1024 * 1024L;
        if (!IMAGE_TYPE.contains(fileSuffix)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持的图片类型");
        }
        if (fileSize > ONE_MB) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件不能大于1MB");
        }
    }

    private void userImportCheck(MultipartFile multipartFile) {
        // 文件大小
        long fileSize = multipartFile.getSize();
        final long FIVE_MB = 1024 * 1024 * 5;

        if (fileSize > FIVE_MB) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件不能大于5MB");
        }
    }

}
